<?php
require("view_header.php");
//1. 允许管理员进入该页面
$isAllowAccess = isset($_SESSION['administrator']);
// 2. 允许机构管理员进入该页面
$isAllowAccess |= isset($_SESSION['jgadmin']);
// 3. 允许学校的管理员创建账号
$isAllowAccess |= isset($_SESSION['xxadmin']);
if (!$isAllowAccess ){
    echo "<a href='../loginpage.php'>Please Login First!</a>";
    exit(1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf8">
    <title> 添加用户 </title>
</head>
<body>
    <div class="container col-sm-8 panel panel-default" style="margin-left: 20px; margin-top: 20px;" id="user_info">
        <!--添加用户用户提示框-->
        <zz-modal-component
                v-if="isShowAddDlg"
                modal-title='添加用户'
                modal-style='width:350px;'
                ok-btn="是"
                cancel-btn="否"
                @on-ok="okAddUser"
                @on-cancel="cancelAddUser">
            <div slot="modal-content">
                <p>添加用户【{{userInfo.name}}】【ID:{{userInfo.id}}】成功，是否继续?</p>
            </div>
        </zz-modal-component>

        <div class="col-sm-12">
            <center><h3>请输入用户的基本信息</h3></center>
        </div>
        <div class="panel-body">
        <form @submit.prevent="postUser()">
            <div class="form-group col-sm-12">
                <label class="col-sm-2 control-label">用户名:</label>
                <div class="col-sm-10">
                    <input v-bind:readonly="isEditUserInfo" v-model="userInfo.id" class="form-control" placeholder="名字拼音首字母,如:wxe" type="text">
                </div>
            </div>
            <div class="form-group col-sm-12">
                <label class="col-sm-2 control-label">姓名:</label>
                <div class="col-sm-10">
                    <input v-model="userInfo.name" class="form-control" placeholder="请输入姓名" type="text">
                </div>
            </div>
            <div class="form-group col-sm-12">
                <label class="col-sm-2 control-label">所在学校:</label>
                <div class="col-sm-10">
                    <input v-model="userInfo.school" class="form-control" placeholder="请输入所在学校" type="text">
                </div>
            </div>
            <div class="form-group col-sm-12">
                <label class="col-sm-2 control-label">联系方式:</label>
                <div class="col-sm-10">
                    <input v-model="userInfo.phone" class="form-control" placeholder="电话、微信、QQ" type="text">
                </div>
            </div>
            <div v-if="!privileges.xxadmin" class="form-group col-sm-12">
                <label class="col-sm-2 control-label">用户类型:</label>
                <div class="col-sm-3">
                    <select v-model="userInfo.type"  @change="userTypeChanged(userInfo.type)" class="form-control">
                        <option value="0" selected>未知</option>
                        <option value ="1">学生</option>
                        <option value ="2">老师</option>
                        <option value ="3">学生家长</option>
                    </select>
                </div>
                <div class="col-sm-3">
                    <select v-model="userInfo.inzuozhu" class="form-control">
                        <option value ="0">非内部人员</option>
                        <option value ="1">内部人员</option>
                    </select>
                </div>
                <div class="col-sm-4" style="color:blue">本机构学生、老师、其他管理人员都属于内部人员</div>
            </div>
            <div v-show="isShowGrade" class="col-sm-12 form-group ">
                <label class="col-sm-2 control-label">年级:</label>
                <div class="col-sm-3">
                    <select v-model="userInfo.grade" class="form-control">
                        <option value="0" selected>未知</option>
                        <option value="4">小学4年级</option>
                        <option value ="5">小学5年级</option>
                        <option value ="6">小学6年级</option>
                        <option value ="7">初中1年级</option>
                        <option value ="8">初中2年级</option>
                        <option value ="9">初中3年级</option>
                        <option value ="10">高中1年级</option>
                        <option value ="11">高中2年级</option>
                        <option value ="12">高中3年级</option>
                    </select>
                </div>
                <div class="col-sm-3">
                    <select v-model="userInfo.upordown" class="form-control">
                        <option value="0" selected>未知</option>
                        <option value ="1">上册</option>
                        <option value ="2">下册</option>
                    </select>
                </div>
            </div>
            <div v-if="!privileges.xxadmin" class="form-group col-sm-12">
                <label class="col-sm-2 control-label">用户状态:</label>
                <div class="col-sm-3">
                    <select v-model="userInfo.status" class="form-control">
                        <option value="0">未知</option>
                        <option value ="1">已试听</option>
                        <option value ="2">学习中</option>
                        <option value ="3">暂停中</option>
                        <option value ="4">已毕业</option>
                    </select>
                </div>
                <div class="col-sm-4" style="color:blue">仅针对于内部学员</div>
            </div>
            <div class="form-group col-sm-12">
                <div class="col-sm-offset-2 col-sm-10">
                    <div class="col-sm-4">
                        <button type="submit" class="form-control btn btn-default" >提交</button>
                    </div>
                    <div class="col-sm-4">
                        <button @click="returnBack()" type="button" class="form-control btn btn-default" >返回上一页</button>
                    </div>
                    <div class="col-sm-4">
                        <button type="reset" class="form-control btn btn-default" >重置</button>
                    </div>
                </div>
            </div>
        </form>
        </div>
    </div>

    <script>
        var app = new Vue({
            el: "#user_info",
            data:{
                userInfo: {
                    id:"",
                    name:"",
                    school:"",
                    phone:"",
                    type:0,
                    inzuozhu:0,
                    grade:0,
                    upordown:0,
                    status:0,
                },
                privileges:{},
                isShowGrade:false,
                isEditUserInfo:false,
                isShowAddDlg:false,
            },

            mounted:function () {
                this.loadUserInfo();
                this.checkPrivileges();
            },

            methods:{

                // here 完善用户编辑逻辑
                // 加载cookie中的用户信息
                loadUserInfo:function() {
                    let jsonStr = getCookie("userInfo");
                    if ( ! isNull(jsonStr) ) {
                        this.isEditUserInfo = true;
                        clearCookie("userInfo");
                        this.userInfo = JSON.parse(jsonStr);
                        this.userTypeChanged(this.userInfo.type);
                    }
                },

                checkPrivileges:function () {
                    let param = "para="+JSON.stringify({op:'check', privi_names:['xxadmin', 'jgadmin']})
                    new Promise(function(resolve, reject) {
                        $.ajax({
                            type:'post',
                            url:'../model/user_router.php',
                            data:param,
                            dataType:'json',
                            success:function (retData) {
                                if ( retData.status ) {
                                    resolve(retData.dat)
                                }
                                else {
                                    reject(retData.msg)
                                }
                            }
                        });

                    }).then(function(privileges) {
                        app.privileges = privileges
                        // 校外管理人员只允许创建学生账号
                        if ( privileges.xxadmin ) {
                            app.userInfo.type = 1
                            app.isShowGrade = true
                        }

                    }).catch(function(errMsg) {

                    })
                },

                userTypeChanged:function(userType) {
                    // -1: 未选择, 1: 学生  2: 老师  3: 学生家长
                    if ( userType == 1 ) {
                        this.isShowGrade = true;
                    }
                    else {
                        this.isShowGrade = false;
                    }
                },

                returnBack:function () {
                    history.go(-1);
                },

                postUser:function() {
                    if ( this.userInfo.id == "" ) {
                        alert('用户名不能为空');
                        return;
                    }
                    if ( this.userInfo.name == "" ) {
                        alert('姓名不能为空');
                        return;
                    }
                    if ( this.userInfo.school == "" ) {
                        alert('所在学校不能为空');
                        return;
                    }
                    if ( this.userInfo.type == -1 ) {
                        alert('请选择用户类型');
                        return;
                    }
                    if ( this.userInfo.type == 1  &&
                        (this.userInfo.grade == -1 || this.userInfo.upordown == -1) ) {
                        alert('请选择学生所在的【年级、上册/下册】');
                        return;
                    }

                    if ( this.isEditUserInfo ) {
                        this.editUser();
                    }
                    else {
                        this.addUser();
                    }
                },

                editUser:function () {
                    let reqData = this.userInfo;
                    reqData.op = "edit";
                    let param = "para="+JSON.stringify(reqData);
                    new Promise(function(resolve, reject) {
                        $.ajax({
                            type:'post',
                            url:'../model/user_router.php',
                            data:param,
                            dataType:'json',
                            success:function (retData) {
                                if ( retData.status ) {
                                    resolve();
                                }
                                else {
                                    reject(retData.msg);
                                }
                            }
                        });

                    }).then(function() {
                        alert('修改用户成功');
                        app.returnBack();
                    }).catch(function(errMsg) {
                        alert('修改用户信息失败:'+errMsg);
                    });
                },

                addUser:function () {
                    let reqData = this.userInfo;
                    reqData.op = "add";
                    let param = "para="+JSON.stringify(reqData);
                    new Promise(function(resolve, reject) {
                        $.ajax({
                            type:'post',
                            url:'../model/user_router.php',
                            data:param,
                            dataType:'json',
                            success:function (retData) {
                                if ( retData.status ) {
                                    resolve();
                                }
                                else {
                                    reject(retData.msg);
                                }
                            }
                        });

                    }).then(function() {
                        // 显示提示框
                        app.isShowAddDlg = true;
                    }).catch(function(errMsg) {
                        alert('添加用户用户信息失败:'+errMsg);
                    });
                },

                okAddUser:function (userInfo) {
                    this.isShowAddDlg = false;
                    this.userInfo.id = "";
                    this.userInfo.name = "";
                    this.userInfo.phone = "";
                },

                cancelAddUser:function () {
                    this.isShowAddDlg = false;
                    this.returnBack();
                },
            }
        });
    </script>
</body>
</html>
<?php
//require("../oj-footer.php");
?>